# source: https://www.lanqiao.cn/problems/2215/learning/ 迪杰斯特拉+堆+变形dist
import os
import sys
from math import inf
import heapq
n, m, s, e = list(map(int, input().strip().split(' ')))
weight = [[] for _ in range(n+1)]
for _ in range(n):
    i, j, g, r, y = list(map(int, input().strip().split(' ')))
    weight[i].append((j, g, r, y, 0))
    weight[j].append((i, g, r, y, 1))

dist = [inf for _ in range(n+1)]
dist[s] = 0

heap = [(0, s)]
heapq.heapify(heap)

while heap:
    dis, v = heapq.heappop(heap)

    for dest, g, r, y, sign in weight[v]:
        cycle = g + r + y
        if not sign:
            wait = cycle - dis%cycle if dis % cycle >= g else 0
        else:
            if g+y <= dis % cycle < g+y+r:
                wait = 0
            elif dis % cycle < g+y:
                wait = g+y - dis%cycle
            else:
                wait = g+y + cycle - dis%cycle
        
        if dist[v] + wait + y < dist[dest]:
            dist[dest] = dist[v] + wait + y
            heapq.heappush(heap, (dist[dest], dest))
    # print(dist)
print(dist[e] if dist[e] != inf else -1)

# source:https://leetcode.cn/submissions/detail/632552161/ 拓扑排序 + 一维dp
class Solution:
    def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
        n = len(colors)
        store = [[] for _ in range(n)]
        for x, y in edges:
            if x == y:
                return -1
            store[x].append(y)
        
        memo = [None]*n
        def dfs(i):
            if memo[i] is not None:
                return memo[i]
            memo[i] = 0
            res = defaultdict(int)
            for v in store[i]:
                c = dfs(v)
                if not c:
                    return c
                for s, val in c.items():
                    res[s] = max(res[s], val)
            res[colors[i]] += 1
            memo[i] = res
            return res

        ans = 0
        for i in range(n):
            res = dfs(i)
            if not res:
                return -1
            ans = max(ans, res[colors[i]])
        return ans

# source:
